package com.ykdl.member.kid.wxapi;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.marketcard.PaySelectActivity;
import com.ykdl.member.kid.marketcard.PaySuccessActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_pay_success);
        
    	api = WXAPIFactory.createWXAPI(this, "wx1594ffac39018603");

        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle("提示");
//			builder.show();
			
			  if (resp.errCode == 0){// 支付成功
				  Toast.makeText(WXPayEntryActivity.this, "支付成功",
							Toast.LENGTH_SHORT).show();
					Intent intent = new Intent(WXPayEntryActivity.this,
							PaySuccessActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					startActivity(intent);
					PaySelectActivity.paySelectActivity.finish();
	            }else if (resp.errCode == -1){// 错误 可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等
	            	Toast.makeText(WXPayEntryActivity.this, "支付失败",
							Toast.LENGTH_SHORT).show();
	            }else if (resp.errCode == -2){//用户取消 	无需处理。发生场景：用户不支付了，点击取消，返回APP。
	            	Toast.makeText(WXPayEntryActivity.this, "取消支付",
							Toast.LENGTH_SHORT).show();
	            }
		}
		
		finish();
	}
}